$().ready(function() {
	chk_login_session();
	$('.sign-out-btn').click(function(){
		
		
	});
	$("input[type='text']").attr("autocomplete","off");
	$("#myForm_login").validate();
	$("#myForm_reg").validate();
	$(".btn-register").click(function(){
		$("input[type='text']").val("");
		$("input[type='password']").val("");
		set_placeholder();
		$(".error-msg").html("&nbsp;");
		//$(".login-part").fadeOut('fast');
		$(".login-box").hide("slide", { direction: "down" }, 1000, function(){
			//$(".login-part").fadeIn('fast');
			$(".register-box").show('slide', { direction: "up" }, 1000, function(){
				$('.register-box').animate({'margin-top':'-10px'}, 300, function(){
					$//("#icons-login").css({'background':'url("'+base_url+'assets/img/crack.png") repeat scroll 0 0 transparent'});
					$('.register-box').animate({'margin-top':'0'}, 300, function(){
						//$('.register-box').animate({'margin-top':'-10px'}, 500, function(){
							//$('.register-box').animate({'margin-top':'0'}, 500, function(){
								//$("#icons-login").animate({opacity: 1}, 500, function() {
							        //$(this).css({'background':'url() repeat scroll 0 0 transparent'})
							    //});								
							//});
						//});
					});
				});
			});
		});
	});
	$(".btn-cancel").click(function(){
		$("input[type='text']").val("");
		$("input[type='password']").val("");
		set_placeholder();
		$(".error-msg").html("&nbsp;");
		//$(".login-part").fadeOut('fast');
		$(".register-box").hide("slide", { direction: "up" }, 1000, function(){
			$(".login-box").show('slide', { direction: "down" }, 1000, function(){
				//$(".login-part").fadeIn('fast');
			});
		});
	});		

	$(".btn-login").click(function(){
		var msg_error = '';
		var strEmail = $("#strEmail").val();
		var strPassword = $("#strPassword").val();
		if($.trim(strEmail)==""){
			msg_error = "Empty email.";
			$('.login-box').append('<div class="result-message-box"></div>');
		    $( ".result-message-box" ).dialog({
		    	title: "Message",
		    	autoOpen: false,
		    	minHeight: 140,
				modal: true,
				draggable: false ,
				show: {
					effect: "explode",
					duration: 300
				},
				hide: {
					effect: "explode",
					duration: 300
				},	      
				buttons: {
					"Ok": function() {
						$(this).dialog("close");
						$( ".result-message-box" ).remove();
					}
				}
		      });
		    $('.ui-dialog-content').text(msg_error);
		    $( ".result-message-box" ).dialog("open");
		} else if($.trim(strPassword)==""){
			msg_error = "Empty password.";
			$('.login-box').append('<div class="result-message-box"></div>');
		    $( ".result-message-box" ).dialog({
		    	title: "Message",
		    	autoOpen: false,
		    	minHeight: 140,
				modal: true,
				draggable: false ,
				show: {
					effect: "explode",
					duration: 300
				},
				hide: {
					effect: "explode",
					duration: 300
				},	      
				buttons: {
					"Ok": function() {
						$(this).dialog("close");
						$( ".result-message-box" ).remove();
					}
				}
		      });
		    $('.ui-dialog-content').text(msg_error);
		    $( ".result-message-box" ).dialog("open");
		} else {
			login_user();
		}
	});
	$(".date_format").blur(function(){
		bday_chkd();		
	});
	$(".date_format_conception").blur(function(){
		conception_chkd();	
	});
	$("#strEmail_reg").blur(function(){
		chk_email_reg();	
	});
	$(".btn-reg").click(function(){
		var msg_error = '';
		var strEmail = $("#strEmail_reg").val();
		var strEmailp = $("#strEmail_reg").attr('placeholder');
		var strName = $("#strName").val();
		var strNamep = $("#strName").attr('placeholder');
		var dateBirthday = $("#dateBirthday").val();
		var dateBirthdayp = $("#dateBirthday").attr('placeholder');
		var dateConceptionDate = $("#dateConceptionDate").val();
		var dateConceptionDatep = $("#dateConceptionDate").attr('placeholder');
		var strPasswordReg = $("#strPasswordReg").val();
		var strPasswordRegC = $("#strPasswordRegC").val();
		var strTerms = $("#strTerms").prop("checked");
		var intChkEmail = $('#intChkEmail').val();
		bday_chkd();
		conception_chkd();
		password_chckd();
		chk_email_reg();
		if($.trim(strName)==""){
			msg_error = "Empty name.";
			
		} else if(strName==strNamep){
			msg_error = "Empty name.";
			
		} else if($.trim(dateBirthday)==""){
			msg_error = "Empty Birthday.";
			
		} else if(dateBirthday==dateBirthdayp){
			msg_error = "Empty Birthday.";
			
		} else if($.trim(strEmail)==""){
			msg_error = "Empty email.";
			
		} else if(strEmail == strEmailp){
			msg_error = "Empty email.";
			
		} else if($.trim(dateConceptionDate)==""){
			msg_error = "Empty date of conception.";
			
		} else if(dateConceptionDate == dateConceptionDatep){
			msg_error = "Empty date of conception.";
			
		} else if($.trim(strPasswordReg)==""){
			msg_error = "Emtpy password.";
			
		} else if(strPasswordReg.length<6 || strPasswordReg.length>10){
			msg_error = "Password character must be 6 to 10.";
			
		} else if(strPasswordReg!=strPasswordRegC){
			msg_error = "Password did not match.";
			
		} else if(strTerms==false){
			msg_error = "Please check terms and conditions.";
			
		} else if(intChkEmail==0){
			msg_error = "Email exist.";
		}else {
			register_user();
		}
		$('.register-box').append('<div class="result-message-box"></div>');
	    $( ".result-message-box" ).dialog({
	    	title: "Message",
	    	autoOpen: false,
	    	minHeight: 140,
			modal: true,
			draggable: false ,
			show: {
				effect: "explode",
				duration: 300
			},
			hide: {
				effect: "explode",
				duration: 300
			},	      
			buttons: {
				"Ok": function() {
					$(this).dialog("close");
					$( ".result-message-box" ).remove();
				}
			}
	      });
	    $('.ui-dialog-content').text(msg_error);
	    $( ".result-message-box" ).dialog("open");
	});	
	$(".toggle-dateBirth, .toggle-dateConceptionDate").hide();
	
	$("#dateBirthday").focus(function(){
		$(".toggle-dateBirthFull").hide();
		$(".toggle-dateBirth").show();
		$('#dateBirthday_d').focus();
	});
	$("#dateConceptionDate").focus(function(){
		$(".toggle-dateConceptionDateFull").hide();
		$(".toggle-dateConceptionDate").show();
		$('#dateConceptionDate_d').focus();
	});	
	
	chk_login();
});
function password_chckd(){
	var strPasswordReg = $("#strPasswordReg").val();
	var regexp = new RegExp(/^[a-zA-Z0-9]+$/);
	var result=regexp.test(strPasswordReg);
	//console.log(result);
}
function bday_chkd(){
	var dateBirthday_d = $("#dateBirthday_d").val();
	var dateBirthday_m = $("#dateBirthday_m").val();
	var dateBirthday_y = $("#dateBirthday_y").val();		

	if(checkdate(dateBirthday_y,dateBirthday_m,dateBirthday_d)==true){
		if(dateBirthday_d.length<2){
			$("#dateBirthday_d").val('0'+dateBirthday_d);
			dateBirthday_d = '0'+dateBirthday_d;
		}
		if(dateBirthday_m.length<2){
			$("#dateBirthday_m").val('0'+dateBirthday_m);
			dateBirthday_m = '0'+dateBirthday_m;
		}
		dateBirthday = dateBirthday_m+"/"+dateBirthday_d+"/"+dateBirthday_y;
		$("#dateBirthday").val(dateBirthday);
		$(".toggle-dateBirth").hide();
		$(".toggle-dateBirthFull").show();
		$(".error-msg").html('');
		var age = getAge(new Date(dateBirthday_y,dateBirthday_m,dateBirthday_d));
		$("#intAge").val(age);		
		$("#dateBirthday_d").val('');
		$("#dateBirthday_m").val('');
		$("#dateBirthday_y").val('');		
	} else {
		//$(".error-msg").html("Please input right date in Birthday.");
		$("#dateBirthday").attr('placeholder','Birthday');
	} 
}
function conception_chkd(){
	var dateConceptionDate_d = $("#dateConceptionDate_d").val();
	var dateConceptionDate_m = $("#dateConceptionDate_m").val();
	var dateConceptionDate_y = $("#dateConceptionDate_y").val();		

	if(checkdate(dateConceptionDate_y,dateConceptionDate_m,dateConceptionDate_d)==true){
		if(dateConceptionDate_d.length<2){
			$("#dateConceptionDate_d").val('0'+dateConceptionDate_d);
			dateConceptionDate_d = '0'+dateConceptionDate_d;
		}
		if(dateConceptionDate_m.length<2){
			$("#dateConceptionDate_m").val('0'+dateConceptionDate_m);
			dateConceptionDate_m = '0'+dateConceptionDate_m;
		}
		dateConceptionDate = dateConceptionDate_m+"/"+dateConceptionDate_d+"/"+dateConceptionDate_y;
		$("#dateConceptionDate").val(dateConceptionDate);
		$(".toggle-dateConceptionDateFull").show();
		$(".toggle-dateConceptionDate").hide();
		$(".error-msg").html('');
		$("#dateConceptionDate_d").val('');
		$("#dateConceptionDate_m").val('');
		$("#dateConceptionDate_y").val('');		
	} else {
		//$(".error-msg").html("Please input right date in Conception Date.");
		$("#dateConceptionDate").attr('placeholder','Date of Conception');
	}	
}
function checkdate(y,m,d){
	m = m-1;	
	var chkd = new Date(y, m, d);
	var chk = 0;
	var cy = chkd.getFullYear();
	var cm = chkd.getMonth();
	var cd = chkd.getDate();

	if(y!=cy){
		chk++;
	}
	if(m!=cm){
		chk++;
	}
	if(d!=cd){
		chk++;
	}	
	if(chk==0){
		return true;
	} else {
		return false;
	}
}
function getAge(dateString) {
	var today = new Date();
	var birthDate = new Date(dateString);
	var age = today.getFullYear() - birthDate.getFullYear();
	var m = today.getMonth() - birthDate.getMonth();
	if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
		age--;
	}
	return age;
}	
function login_user(){
	var strEmail = $("#strEmail").val();
	var strPassword = $("#strPassword").val();
	
	xfunc = "login/login_user";
	
	data_sent = {
		'strEmail':strEmail,
		'strPassword':strPassword
	};
	
	$.ajax({ 
		type: "POST", // request type POST
		url: base_url+xfunc, // post to PHP file
		data: data_sent, // request send to PHP file
		dataType:'json',
		success: function(result){ // result = return from php request

			$('.login-box').append('<div class="result-message-box"></div>');
		    $( ".result-message-box" ).dialog({
		    	title: "Message",
		    	autoOpen: false,
		    	minHeight: 140,
				modal: true,
				draggable: false ,
				show: {
					effect: "explode",
					duration: 300
				},
				hide: {
					effect: "explode",
					duration: 300
				},	      
				buttons: {
					"Ok": function() {
						$(this).dialog("close");
						$( ".result-message-box" ).remove();
					}
				}
		      });
		    $('.ui-dialog-content').text(result.message);
		    $( ".result-message-box" ).dialog("open");
			//$(".error-msg").html(result.message);
			if(result.chk_log==1){
				var strRememberMe = $('#strRememberMe').prop("checked");
				if(strRememberMe==true){
					$.cookie('owner', result.owner, { expires: 9999 });
					$.cookie('chk_log', result.chk_log, { expires: 9999 });
					$.cookie('intAccountType', result.intAccountType, { expires: 9999 });
					$.cookie('session_code', result.session_code, { expires: 9999 });
					$.cookie('dateConceptionDate', result.dateConceptionDate, { expires: 9999 });
					$.cookie('strImgLink', result.strImgLink, { expires: 9999 });
				} else {
					$.cookie('owner', result.owner, { expires: 1 });
					$.cookie('chk_log', result.chk_log, { expires: 1 });
					$.cookie('intAccountType', result.intAccountType, { expires: 1 });
					$.cookie('session_code', result.session_code, { expires: 1 });
					$.cookie('dateConceptionDate', result.dateConceptionDate, { expires: 1 });
					$.cookie('strImgLink', result.strImgLink, { expires: 1 });
				}
				var strName = $.cookie('strName');	
				$("input[type='text']").val("");
				$("input[type='password']").val("");
				$(".error-msg").html("&nbsp;");
								
				$('.login-part').hide("slide", { direction: "left" }, 1000, function(){
					
				});
				
				$(".login-box").hide("slide", { direction: "right" }, 1000,function(){	
					$('.h-wrapper-box').show();
					$('.box-wrapper-panel').show();					
					$(".panel-box").fadeIn('slow',function(){
						//$('.login-part').fadeIn('slow',function(){});
						//$('.box-wrapper-icons').hide();
						$('.box-wrapper-login').fadeIn('slow',function(){
							$('#admin_name').html('Welcome '+strName);
						});
					});
				});
				
			} else {
				//block = $('.login-part').attr('display');
				//if(block == "none"){
					$(".login-box").show('slide', { direction: "down" }, 1000, function(){
						//$(".login-part").fadeIn('fast');
						$('.box-wrapper-icons').show();
						$('.box-wrapper-login').show();
						$('.h-wrapper-box').hide();
					});					
				//}
			}
		}
	});	
}
function register_user(){
	var strName = $("#strName").val();
//	var dateBirthday_d = $("#dateBirthday_d").val();
//	var dateBirthday_m = $("#dateBirthday_m").val();
//	var dateBirthday_y = $("#dateBirthday_y").val();
	var strPasswordReg = $("#strPasswordReg").val();
	var dateBirthday = $("#dateBirthday").val();

	//var dateBirthday = dateBirthday_m+"/"+dateBirthday_d+"/"+dateBirthday_y;
	
	var intAge = $("#intAge").val();
	var strEmail = $("#strEmail_reg").val();

	var dateConceptionDate = $("#dateConceptionDate").val();
	bday = dateBirthday.split("/");
	cdate = dateConceptionDate.split("/");
	dateBirthday = bday[2]+'-'+bday[0]+'-'+bday[1];
	dateConceptionDate = cdate[2]+'-'+cdate[0]+'-'+cdate[1];
	xfunc = "register/register_user";
	
	data_sent = {
		'strName':strName,
		'dateBirthday':dateBirthday,
		'intAge':intAge,
		'strEmail':strEmail,
		'dateConceptionDate':dateConceptionDate,
		'strPassword':strPasswordReg,
	};
	
	$.ajax({ 
		type: "POST", // request type POST
		url: base_url+xfunc, // post to PHP file
		data: data_sent, // request send to PHP file
		dataType:'json',
		success: function(result){ // result = return from php request
			if(result.fail==0){
				$("input[type='text']").val("");
				$("input[type='password']").val("");
				$(".login-part").fadeOut('fast');
//				$(".register-box").hide("slide", { direction: "up" }, 1000, function(){
//					$(".login-box").show('slide', { direction: "down" }, 1000, function(){
//						$(".login-part").fadeIn('fast');
//					});
//				});	
				$(".register-box").fadeOut('fast');
				$(".error-msg").html(result.message+' Password has been sent to your email, Thank you.');
				send_password(result);
				$("#strEmail").val(strEmail);
				$("#strPassword").val(strPasswordReg);
				login_user()
			} else {
				$//('.register-box').append('<div class="result-message-box"></div>');
			    $( ".result-message-box" ).dialog({
			    	title: "Message",
			    	autoOpen: false,
			    	minHeight: 140,
					modal: true,
					draggable: false ,
					show: {
						effect: "explode",
						duration: 300
					},
					hide: {
						effect: "explode",
						duration: 300
					},	      
					buttons: {
						"Ok": function() {
							$(this).dialog("close");
							$( ".result-message-box" ).remove();
						}
					}
			      });
			    $('.ui-dialog-content').text(result.message);
			    $( ".result-message-box" ).dialog("open");
				//$(".error-msg").html(result.message);
			}
		}
	});	
}
function send_password(result){
	var strEmail = result.strEmail;
	var strKeys = result.strKeys;
	var strName = result.strName;
	
	xfunc = "register/send_password";
	
	data_sent = {
			'strEmail':strEmail,
			'strKeys':strKeys,
			'strName':strName
		};
		
		$.ajax({ 
			type: "POST", // request type POST
			url: base_url+xfunc, // post to PHP file
			data: data_sent, // request send to PHP file
			dataType:'json',
			success: function(result){ // result = return from php request

			}
		});	
}
function chk_login(){
	var owner = $.cookie('owner');
	var chk_log = $.cookie('chk_log');
	
	if(chk_log==1){
		$('.h-wrapper-box').show();
		$("input[type='text']").val("");
		$("input[type='password']").val("");
		$(".error-msg").html("&nbsp;");
						
		//$('.login-part').fadeIn('slow', function(){});
		
		$(".login-box").fadeOut('slow',function(){	
			$('.box-wrapper-icons').hide();
			$(".panel-box").fadeIn('slow',function(){
				$('.box-wrapper-login').hide();
			});
		});
		
	} else {
		$('.h-wrapper-box').hide();
		//$('.box-wrapper-icons').show();
	}
}
function get_hash(){
	if(window.location.hash) {
		var func = window.location.hash;
		func = func.substring(1, func.length)
		$('#hash_btn').attr('onclick',func+"();");
		var chk_log = $.cookie('chk_log');
		if(chk_log==1){
			setTimeout("click_func()",1000);
		}
	} else {
	  // Fragment doesn't exist
	}
}
function click_func(){
	$('.hide').hide();
	$('#hash_btn').click();
}
function chk_email_reg(){
	var strEmail = $("#strEmail_reg").val();
	
	xfunc = "register/chk_email";
	
	data_sent = {
			'strEmail':strEmail,
		};
		
		$.ajax({ 
			type: "POST", // request type POST
			url: base_url+xfunc, // post to PHP file
			data: data_sent, // request send to PHP file
			dataType:'json',
			success: function(result){ // result = return from php request
				if(result.strEmail==0){
					$('#intChkEmail').val(1);
				} else {
					$('#intChkEmail').val(0);
					//$(".error-msg").html("Email exist.");
				} 
			}
		});	
}
function chk_login_session(){
	var chk_log = $.cookie('chk_log');
	if(chk_log!=1){
		$('.splash-box').show();
		//$('.box-wrapper-icons').show();
		setTimeout(function(){ 
					$('.splash-box').fadeOut("slow",function(){
						$(".box-wrapper-splash").hide();
						$('.box-wrapper-login').show();
						$('.login-box').fadeIn("slow", function(){
							$('.box-wrapper-icons').show();
						});
					});
				},
				1000);
	} else {
		$('.login-box').hide();
		$('.box-wrapper-splash').hide();
		$('.box-wrapper-panel').show();
		$('.box-wrapper-icons').show();
	}
}
